package com.changgou.content.controller;

import com.changgou.FastDFSFile;
import com.changgou.util.FastDFSClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@CrossOrigin // 开启跨域
public class FileController {

    
    /**
     * @author ShortDust
     * @Description 文件上传
     * @ClassName FileController
     * @date 2019/11/16 9:54
     * @return 
    */
    @PostMapping("/upload")
    public String upload(@RequestParam("feign") MultipartFile file) throws IOException {
        //文件上传需要的内容
        String name = file.getOriginalFilename();
        byte[] content = file.getBytes();
        String ext = FilenameUtils.getExtension(name);
        String md5 = "1";
        String author = "gouwa";
        //上传文件 uploadFile
        FastDFSFile fastDFSFile = new FastDFSFile(name, content, ext, md5, author);
        String[] uploadResult = FastDFSClient.uploadFile(fastDFSFile);
        //获取文件上传的地址
        String url = FastDFSClient.getUrl();
        url = url + "/" + uploadResult[0] + "/" + uploadResult[1];
        return url;
    }
}
